package com.mcintyret.site.web.controller.journal;

import com.mcintyret.site.service.journal.JournalFamilyHelper;
import com.mcintyret.site.web.controller.journal.forms.JournalFamilyForm;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * User: mcintyret2
 * Date: 06/04/2012
 */

@Controller
public class JournalFamilyController extends BaseJournalController {

    @Resource
    private JournalFamilyHelper journalFamilyHelper;

    @RequestMapping(value = "/{origin}/create-journal-family", method = RequestMethod.POST)
    private String createJournalFamily(
            @Valid JournalFamilyForm journalFamilyForm,
            BindingResult bindingResult,
            @PathVariable String origin) {

        String redirectString = "redirect:/" + origin;

        if (!bindingResult.hasErrors()) {
            Integer id = journalFamilyHelper.addNewJournalFamily(journalFamilyForm);
            redirectString += "#journal_family_" + id;
        }
        return redirectString;
    }

    @RequestMapping("/{origin}/delete-journal-family")
    private String deleteJournalFamily(
            @RequestParam("id") int journalFamilyId,
            @PathVariable String origin) {
        journalFamilyHelper.deleteJournalFamilyWithId(journalFamilyId);

        return "redirect:/" + origin;
    }

}
